package jmysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  
public class query {
	 
	private String fields="select ";
	private String tables=" from ";
	private String constrains=" where ";
	private String extra=" ";

	private Connection conexion = null;
	PreparedStatement ps;
	public MysqlDataSource dataSource;
	
	public query(){
	
		  dataSource = new MysqlDataSource();
          dataSource.setUser("root");
          dataSource.setPassword("fagoner");
          dataSource.setDatabaseName("udsv1");
          dataSource.setServerName("localhost:3306"); 
          try { 
              Connection conexion = (Connection) dataSource.getConnection();
              Statement st=(Statement) conexion.createStatement();
              System.out.println("Conexion lograda");

              st.close();
              conexion.close();
          }
          catch(SQLException e){
              
          }
		 
//		try {  
//			conexion=DriverManager.getConnection("jdbc:mysql://localhost/udsv1?useServerPrepStmts=true",
//                    "root", "fagoner");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
	} 
	
	
	public void insert(){
		 try {
			 for(int i=2;i<=5;i++){
				 PreparedStatement ps = conexion
	                   .prepareStatement("insert into person values (?,?)");
	            ps.setInt(1,i);
	            ps.setString(2,"Digamos_"+i);
	            ps.executeUpdate();
			 }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	}
	
	
	//Revisa si existe una area
	public boolean Existe(String nomarea){
		try {
			if (null == ps) {
                ps= conexion
                        .prepareStatement("select nombre from area where nomarea=?");
            }
            ps.setString(1, nomarea);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) 
                return true;
			} catch (SQLException e) {
				e.printStackTrace();
		}
		return false;
	}
	
	
		
	public void selectPreparedStatement(int clave) {
		try {
	            if (null == ps) {
	                ps= conexion
	                        .prepareStatement("select * from person where id>?");
	            }
	            ps.setInt(1, clave);
	            ResultSet rs = ps.executeQuery();
	            while (rs.next()) {
	                System.out.println("Clave=" + rs.getString(1) + " nombre="+rs.getString(2));
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	}
	 
	 
	//se agregan los campos 
	public void Fields(String fields_[]){
		for(String f:fields_)
			fields+=f+" ";
	}
		
	public void Tables(String tables_[]){
		for(String t:tables_)
			tables+=t+" ";
	}
	
	public void Cont(String constrains_[]){
		for(String c:constrains_)
			constrains+=c+" ";
	}
	
	public void Extra(String extra_[]){
		for(String e:extra_)
			extra+=e+" ";
	}
	
	public String QueryStatement(){
		return fields+tables+constrains+extra;
	}

	protected void finalize(){
		if(this.conexion!=null)
			try {
				conexion.close();
				conexion=null;
				fields=tables=constrains=extra=null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
}
